package Task4;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) {
        ServerSocket ss = null;
        Socket s = null;
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        try {
            ss = new ServerSocket(8888);
            System.out.println("等待客户端的链接请求");
            s = ss.accept();
            System.out.println("客户端链接成功");
            ois = new ObjectInputStream(s.getInputStream());
            Object obj = ois.readObject();
            UserMessage um = (UserMessage) obj;
            if (um.user.getId().equals("admin") && um.user.getPassword().equals("123456")){
                um.setType("success");
                System.out.println("账号密码正确");
            }else {
                um.setType("fail");
                System.out.println("账号密码错误");
            }
            oos = new ObjectOutputStream(s.getOutputStream());
            oos.writeObject(um);
            System.out.println("向客户端返回UserMessage成功");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (null != oos) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != ois) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != s) {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != ss) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
